package futbol.tacticas.concursantes.qualitycode.common;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.List;

import futbol.General;

public class Campo
{
  public static final int OFFSETX = 160;
  public static final int OFFSETY = 120;
  
  private Celda[][] celdas;
  private int distPeligro = 60;
  private int distBola = 170;
  
  public Campo(int distPeligro, int x, int y, int x2, int y2, int separacion, int ancho, int alto)
  {
    this.distPeligro = distPeligro;    
    int cellsX = 0;
    int cellsY = 0;
    
    int celdaX = x;    
    final List<Celda> lstCeldas = new ArrayList<Celda>();
    for (;x2 < celdaX-separacion;)
    {
      int celdaY = y-separacion;      
      cellsY = 0;
      for (;y2 < celdaY-separacion;)
      {        
        lstCeldas.add(new Celda(cellsX, cellsY, celdaX, celdaY, ancho, alto));
        celdaY += -alto - separacion;
        cellsY++;
      }
      cellsX++;
      celdaX += -ancho - separacion;
    }
    
    celdas = new Celda[cellsX][cellsY];
    for (Celda celda : lstCeldas)
      celdas[celda.idxX][celda.idxY] = celda;
  }
  
  public void setDistanciaBola(int maxDistBola)
  {
    this.distBola = maxDistBola;
  }

  private void reset()
  {
    for (int i = 0; i < celdas.length; i++)
      for (int j = 0; j < celdas[0].length; j++)
        celdas[i][j].reset();
  }

  public Celda getCelda(QPlayer player)
  {
    for (int i = 0; i < celdas.length; i++)
      for (int j = 0; j < celdas[0].length; j++)
      {
        Celda c = celdas[i][j];
        if (c.isContains(player))
          return c;
      }
    
    return null;
  }
  
  public Celda estaSeguro(Celda celda)
  {    
    return null;
  }
  
  public Point mejorCelda(Point pos, int minDist)
  {
    Celda mejor = null;
    double dist = 0;
    for (int i = 0; i < celdas.length; i++)
      for (int j = 0; j < celdas[0].length; j++)
      {
        Celda c = celdas[i][j];
        if (c.isSegura())
        {
          if (mejor == null)
          {
            if (dist>= minDist)
            {
              mejor = c;
              dist = General.getDistancia(c.area.getCenterX(), c.area.getCenterY(), pos.getX(), pos.getY());
            }
          }
          else
          {        
            double dist2 = General.getDistancia(c.area.getCenterX(), c.area.getCenterY(), pos.getX(), pos.getY());
            if (dist2 >= minDist && dist2 < dist)
            {
              dist = dist2;
              mejor = c;
            }
          }
        }
      }
      if (mejor == null)
        return null;
      
      return new Point((int)mejor.area.getCenterX(), (int)mejor.area.getCenterY());
  }

  public void update(QBall ball, List<? extends QPlayer> opponents)
  {
    reset();
    for (int i = 0; i < celdas.length; i++)
      for (int j = 0; j < celdas[0].length; j++)
      {
        celdas[i][j].updateBall(ball);
        if (celdas[i][j].isSegura())
        {
          for (QPlayer player : opponents)
            celdas[i][j].updateOpponets(player);
        }
      }
  }
  
  public void invalidate(QPlayer player)
  {
    for (int i = 0; i < celdas.length; i++)
      for (int j = 0; j < celdas[0].length; j++)
        if (celdas[i][j].isSegura())
          celdas[i][j].updateOpponets(player);
  }
  
  public void pintar(boolean over, Graphics gdb)
  {
    if (!over)
      for (int i = 0; i < celdas.length; i++)
        for (int j = 0; j < celdas[0].length; j++)
          celdas[i][j].pintar(over, gdb);
  }
  
  public class Celda
  {        
    private static final int BEST = 255; 
    public final Rectangle area;
    public int disponibilidad = BEST;
    public int idxX;
    public int idxY;
    
    public Celda(int idxX, int idxY, int x, int y, int ancho, int alto)
    {
      this.idxX = idxX;
      this.idxY = idxY;
      //area = new Rectangle((x*-1)+OFFSETX, (y*-1)+OFFSETY, ancho, alto);
      area = new Rectangle(x, y, ancho, alto);
    }
    
    public boolean isContains(QPlayer player)
    {
      return area.intersects(player.getX(), player.getY(), 20, 20);
    }

    public boolean isSegura()
    {
      return disponibilidad == BEST;
    }

    public void reset()
    {
      disponibilidad = BEST;
    }

    public Point mejorCelda(Point pos)
    {       
      return null;
    }

    public void updateBall(QBall ball)
    {
      int d = (int) General.getDistancia(area.x-10, area.y-10, ball.getX(), ball.getY());
      if (d > 170)
        disponibilidad = 10;        
    }
    
    public void updateOpponets(QPlayer opponent)
    { 
      if (disponibilidad == 10)
        return;
      
      if (area.intersects(opponent.getX(), opponent.getY(), 20, 20))
      {
        disponibilidad = 10;
      }
      else
      {        
        int d = (int) General.getDistancia(area.getCenterX()-5, area.getCenterY()-5, opponent.getX(), opponent.getY());
        if (d < distPeligro)
          disponibilidad = 10;
      }/**/
    }
    
    public void pintar(boolean over, Graphics gdb)
    {
      //gdb.setColor(Color.WHITE);
      //gdb.drawString(" "+disponibilidad, 141+160-area.x, 91+120-area.y);
      
      //if (disponibilidad > 0)
      {
        gdb.setColor(new Color(255-disponibilidad,disponibilidad,0));      
        gdb.fillRect(141+160-area.x, 91+120-area.y, area.width, area.height);
      }
    }
  }

}












